package org.gdstash.ui.select;

import org.gdstash.db.DBViewItemSkill;
import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.List;

public class ItemSkillPane extends AdjustablePanel {
    private static List<DBViewItemSkill> skills;
    private static DBViewItemSkill[] itemSkills;
    private JComboBox<DBViewItemSkill> cbSkills;

    public static void loadSkills() {
        final String s = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_SKILL_ALL");
        if (GDStashFrame.dbConfig == null || !GDStashFrame.dbConfig.gddbInit) {
            (ItemSkillPane.itemSkills = new DBViewItemSkill[2])[0] = null;
            final DBViewItemSkill all = new DBViewItemSkill(s);
            ItemSkillPane.itemSkills[1] = all;
            return;
        }
        ItemSkillPane.skills = DBViewItemSkill.getItemSkills();
        if (ItemSkillPane.skills == null) {
            (ItemSkillPane.itemSkills = new DBViewItemSkill[1])[0] = null;
        } else {
            (ItemSkillPane.itemSkills = new DBViewItemSkill[ItemSkillPane.skills.size() + 2])[0] = null;
            final DBViewItemSkill all = new DBViewItemSkill(s);
            ItemSkillPane.itemSkills[1] = all;
            int pos = 2;
            for (final DBViewItemSkill skill : ItemSkillPane.skills) {
                ItemSkillPane.itemSkills[pos] = skill;
                ++pos;
            }
        }
    }

    static {
        ItemSkillPane.skills = null;
        ItemSkillPane.itemSkills = null;
        loadSkills();
    }

    @Override
    public void adjustUI() {
        int size = 12;
        if (GDStashFrame.iniConfig != null) {
            size = GDStashFrame.iniConfig.sectUI.fontSize;
        }
        final Dimension dimMax = new Dimension(50 * size, 2 * size);
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCombo = UIManager.getDefaults().getFont("ComboBox.font");
        if (fntCombo == null) {
            fntCombo = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCombo = fntCombo.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_ITEM_SKILL"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbSkills == null) {
            this.cbSkills = new JComboBox<DBViewItemSkill>(ItemSkillPane.itemSkills);
        }
        this.cbSkills.setFont(fntCombo);
        this.cbSkills.setPreferredSize(dimMax);
        this.cbSkills.setMaximumSize(dimMax);
    }

    public void updateItemSkills() {
        final DefaultComboBoxModel<DBViewItemSkill> model = new DefaultComboBoxModel<DBViewItemSkill>(ItemSkillPane.itemSkills);
        this.cbSkills.setModel(model);
    }

    public void clear() {
        this.cbSkills.getModel().setSelectedItem(null);
    }

    public void addCriteria(final SelectionCriteria criteria) {
        final int index = this.cbSkills.getSelectedIndex();
        criteria.allItemSkills = false;
        criteria.itemSkill = null;
        if (index == 1) {
            criteria.allItemSkills = true;
        } else {
            final DBViewItemSkill skill = (DBViewItemSkill) this.cbSkills.getSelectedItem();
            if (skill != null) {
                criteria.itemSkill = skill.getSkillID();
            }
        }
    }

    public ItemSkillPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbSkills));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbSkills));
        layout.setVerticalGroup(vGroup);
    }
}
